<?php

namespace App\Api\Controllers;

use App\Api\Controllers\BaseController;

use App\Api\Service\UserService;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;


class LoginController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->service = new UserService();
    }

    /**
     * 微信小程序登录
     *
     * @return void
     */
    public function login(Request $request)
    {
        $code = $request->input('code', '');
        $iv = $request->input('iv', '') ?? '';
        $encryptedData = $request->input('encryptedData', '') ?? '';
        $parent_id = $request->input('parent_id', 0) ?? 0;
        $user = $this->service->wxmini_login($code, $iv, $encryptedData, $parent_id);
        return success('登录成功', $this->service->login('openid', $user->openid, 'third_party'));
    }

    public function login_test(Request $request)
    {
        $id = $request->input('id', '');
        return success('登录成功', $this->service->login('id', $id, 'password'));
    }

    public function test()
    {
        $data = [
            'search_criteria' => [
                'sex' => "1"
            ],
            'uid' => 11
        ];
//        $response = Http::post('http://120.53.247.244:5000/search_list', $data);
//        $response = json_decode($response);
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://120.53.247.244:5000/search_list',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($data),
            CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        return success('Test', json_decode($response));
    }
}
